package com.example.demo.controller;

import com.example.demo.domain.CourseInfo;
import com.example.demo.domain.Student;
import com.example.demo.service.StudentService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RestController组合注解，相当于@Controller+@ResposeBody
//@RestController
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private Gson gson;

    //MVC模式
//-------------------------1.thymleaf模板填充----------------------------
    @GetMapping("/list")
    public String getList(Model model) {
        //先获取数据
        List<Student> all = studentService.findAll();
        //再存数据到model中,以便后面在html中可以访问
        model.addAttribute("result", all);
        return "/student/list";
    }

    @CrossOrigin
//    @GetMapping("/byName")
//    public String findStudentByName(@RequestParam("studentName") String StudentName, Model model) {
//        List<Student> student = studentService.findByStudentNameLike(StudentName);
//        model.addAttribute("result", student);
//        return "/student/list";
//    }

    //@RequestMapping注解告知spring这个类或者函数映射到那个url
    @GetMapping(value = "/findStudent")
    public String findStudent() {
        List<Student> myAll = studentService.findAll();
        String jsonStr = gson.toJson(myAll);
        return jsonStr;
    }

    @GetMapping(value = "/courseSelectionById/{id}")
    public String findCourseSelection(@PathVariable String id) {
        List<CourseInfo> myAll = studentService.findCourseSelectionById(id);
        String jsonStr = gson.toJson(myAll);
        return jsonStr;
    }
}
//    --------------------------2.ajax数据填充---------------------------------------
//    public String getList(){
//        List<Student> all = studentService.findAll();
//        return "/student/list";
//    }
//}


//}


